<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <canvas id="canvas"></canvas>
    <script src="./js/jquery-1.12.4.js"></script>
    <script>
        function Particles() {
            this.color = [
                '255,255,255',
                '255,99,71',
                '19,19,19'
            ];
            this.canvas = $('#canvas')[0];
            this.ctx = this.canvas.getContext('2d');
            this.numParticles = 75;
            this.minOpacity = 0.005;
            this.maxOpacity = 0.5;
            this.minSpeed = 0.05;
            this.maxSpeed = 0.5;
            this.minRadius = 5;
            this.maxRadius = 35;
        }

        Particles.prototype = {
            init() {
                var _self = this;
                this.render();
                this.createCircle();
                // $(window).on('resize', function () {
                //     _self.render();
                // });
            },
            _rand(min, max) {
                return Math.random() * (max - min) + min;
            },
            render() {
                var _self = this,
                    wWidth = $(window).width(),
                    wHeight = $(window).height();
                _self.canvas.width = wWidth;
                _self.canvas.height = wHeight;
            },
            createCircle() {
                var particles = [];
                var _self = this;
                for (var i = 0; i < _self.numParticles; i++) {
                    particles[i] = {
                        color: 'rgba(' + _self.color[Math.floor(Math.random() * _self.color.length)] + ',' + _self._rand(_self.minOpacity, _self.maxOpacity) + ")",
                        randius: _self._rand(_self.minRadius, _self.maxRadius),
                        xPos: _self._rand(0, _self.canvas.width),
                        yPos: _self._rand(0, _self.canvas.height),
                        xVelocity: _self._rand(_self.minSpeed, _self.maxSpeed),
                        yVelocity: _self._rand(_self.minSpeed, _self.maxSpeed),
                    }
                    // console.log(particles);
                    // _self.draw(particles[i]);

                }
                _self.animate(particles);
            },
            draw(particle) {
                var _self = this,
                    ctx = _self.ctx,
                    item = particle;
                //     grd = ctx.createRadialGradient(item.xPos, item.yPos, item.randius * 2, item.xPos, item.yPos, item.randius / 2);
                // grd.addColorStop(0, item.color);
                // grd.addColorStop(1, 'rgba(34,34,34,0.3)');
                // ctx.fillStyle = grd;
                ctx.strokeStyle = item.color;
                // ctx.strokeStyle = grd;
                ctx.lineWidth = 3;
                ctx.beginPath();
                ctx.arc(item.xPos, item.yPos, item.randius, 0, 2 * Math.PI, false);
                // ctx.fill();
                ctx.stroke();
            },
            animate(particles) {
                var _self = this,
                    ctx = _self.ctx,
                    item = particle;

                setInterval(function () {
                    ctx.fillStyle = "rgb(255,255,255)";
                    ctx.fillRect(0, 0, _self.canvas.width, _self.canvas.height);
                    for (var i = 0; i < particles.length; i++) {
                        particles[i].xPos += particles[i].xVelocity;
                        particles[i].yPos += particles[i].yVelocity;
                        _self.draw(particles[i]);
                    }
                }, 40)
            }
        }

        var particle = new Particles();

        particle.init();



    </script>

</body>

</html>